package com.example.food;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private RadioGroup myradioGroup;
    private RadioButton rbutton1, rbutton2, rbutton3, rbutton4;
    private Resources res;
    private Drawable iconHomeTrue, iconHomeFalse, iconCommunityTrue, iconCommunityFalse, iconOrderTrue, iconOrderFalse, iconMeTrue, iconMeFalse;
    private int fontColorFalse, fontColorTrue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.setStatusBarColor(getResources().getColor(android.R.color.white));
        }

        initview();
        navigation();
    }

    private void initview() {
        myradioGroup = findViewById(R.id.main_radioGroup);
        rbutton1 = findViewById(R.id.main_radio0);
        rbutton2 = findViewById(R.id.main_radio1);
        rbutton3 = findViewById(R.id.main_radio2);
        rbutton4 = findViewById(R.id.main_radio3);
        res = getResources();

        iconHomeTrue = res.getDrawable(R.drawable.icon_home_true);
        iconHomeFalse = res.getDrawable(R.drawable.icon_home_false);
        iconCommunityTrue = res.getDrawable(R.drawable.icon_community_true);
        iconCommunityFalse = res.getDrawable(R.drawable.icon_community_false);
        iconOrderTrue = res.getDrawable(R.drawable.icon_order_true);
        iconOrderFalse = res.getDrawable(R.drawable.icon_order_false);
        iconMeTrue = res.getDrawable(R.drawable.icon_me_true);
        iconMeFalse = res.getDrawable(R.drawable.icon_me_false);
        fontColorFalse = res.getColor(R.color.navigation_false);
        fontColorTrue = res.getColor(R.color.p_green);

        setAllImage();
    }

    private void setAllImage() {
        rbutton1.setCompoundDrawablesWithIntrinsicBounds(null, iconHomeFalse, null, null);
        rbutton2.setCompoundDrawablesWithIntrinsicBounds(null, iconCommunityFalse, null, null);
        rbutton3.setCompoundDrawablesWithIntrinsicBounds(null, iconOrderFalse, null, null);
        rbutton4.setCompoundDrawablesWithIntrinsicBounds(null, iconMeFalse, null, null);
    }

    private void navigation() {
        myradioGroup.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            setAllImage(); // Reset all images to false state

            RadioButton checkedButton = null;
            switch (checkedId) {
                case R.id.main_radio0:
                    checkedButton = rbutton1;
                    break;
                case R.id.main_radio1:
                    checkedButton = rbutton2;
                    break;
                case R.id.main_radio2:
                    checkedButton = rbutton3;
                    break;
                case R.id.main_radio3:
                    checkedButton = rbutton4;
                    break;
            }

            if (checkedButton != null) {
                checkedButton.setTextColor(fontColorTrue);
                switch (checkedId) {
                    case R.id.main_radio0:
                        checkedButton.setCompoundDrawablesWithIntrinsicBounds(null, iconHomeTrue, null, null);
                        Toast.makeText(MainActivity.this, "首页", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.main_radio1:
                        checkedButton.setCompoundDrawablesWithIntrinsicBounds(null, iconCommunityTrue, null, null);
                        Toast.makeText(MainActivity.this, "吃货驾到", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.main_radio2:
                        checkedButton.setCompoundDrawablesWithIntrinsicBounds(null, iconOrderTrue, null, null);
                        Toast.makeText(MainActivity.this, "我的订单", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.main_radio3:
                        checkedButton.setCompoundDrawablesWithIntrinsicBounds(null, iconMeTrue, null, null);
                        Toast.makeText(MainActivity.this, "个人中心", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }
}